<template>
  <div class="login-page">
    <div class="background-animation"></div>
    <div class="brand-mark">天境数字人</div>
    <div class="login-container">
      <div class="logo-icon">
        <div class="logo-circle"></div>
      </div>
      <h2>欢迎登录</h2>
      <el-form :model="loginForm" @submit.prevent="handleLogin">
        <el-form-item>
          <el-input 
            v-model="loginForm.phone" 
            placeholder="请输入手机号" 
            prefix-icon="el-icon-mobile"
            class="custom-input"
          />
        </el-form-item>
        <el-form-item>
          <el-input 
            v-model="loginForm.password" 
            type="password" 
            placeholder="请输入密码" 
            prefix-icon="el-icon-lock"
            class="custom-input"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleLogin" class="login-button">
            <span class="button-text">登录</span>
            <div class="button-glow"></div>
          </el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="text" @click="goToRegister" class="register-link">
            没有账号？<span class="highlight">注册</span>
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
import { apiRequest, API_CONFIG } from '../config/api'

const router = useRouter()

const loginForm = ref({
  phone: '',
  password: ''
})

const handleLogin = async () => {
  if (loginForm.value.phone && loginForm.value.password) {
    try {
      const response = await apiRequest(API_CONFIG.ENDPOINTS.LOGIN_BY_PHONE, {
        method: 'POST',
        body: JSON.stringify({
          phone: loginForm.value.phone,
          password: loginForm.value.password
        }),
        headers: {
          'Content-Type': 'application/json'
        }
      })

      if (response.code === 200) {
        localStorage.setItem('token', response.data.token)
        localStorage.setItem('userPhone', loginForm.value.phone)
        ElMessage.success('登录成功')
        router.push('/') // 登录成功后跳转到首页
      } else {
        ElMessage.error(response.msg || '登录失败，请检查手机号和密码')
      }
    } catch (error) {
      console.error('登录请求失败:', error)
      ElMessage.error('登录失败，请重试')
    }
  } else {
    ElMessage.error('请输入手机号和密码')
  }
}

const goToRegister = () => {
  console.log('点击注册按钮')
  window.location.href = '/register'
}
</script>

<style scoped>
.login-page {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  width: 100%;
  background: linear-gradient(135deg, #0a0a0a 0%, #1a1a2e 25%, #16213e 50%, #1a1a2e 75%, #0a0a0a 100%);
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  overflow: hidden;
}

.background-animation {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: 
    radial-gradient(circle at 20% 80%, rgba(102, 126, 234, 0.1) 0%, transparent 50%),
    radial-gradient(circle at 80% 20%, rgba(118, 75, 162, 0.1) 0%, transparent 50%),
    radial-gradient(circle at 40% 40%, rgba(102, 126, 234, 0.05) 0%, transparent 50%);
  animation: background-float 20s ease-in-out infinite;
}

@keyframes background-float {
  0%, 100% {
    transform: translate(0, 0) scale(1);
  }
  33% {
    transform: translate(-30px, -30px) scale(1.1);
  }
  66% {
    transform: translate(30px, 30px) scale(0.9);
  }
}

.login-container {
  width: 420px;
  padding: 50px 40px;
  background: linear-gradient(135deg, rgba(26, 26, 46, 0.95), rgba(22, 33, 62, 0.95));
  backdrop-filter: blur(20px);
  border-radius: 20px;
  box-shadow: 
    0 20px 60px rgba(0, 0, 0, 0.8),
    0 0 0 1px rgba(102, 126, 234, 0.15),
    inset 0 1px 0 rgba(255, 255, 255, 0.1);
  margin: auto;
  border: 1px solid rgba(102, 126, 234, 0.2);
  position: relative;
  z-index: 10;
  transition: all 0.3s ease;
  color-scheme: dark;
}

.login-container:hover {
  transform: translateY(-2px);
  box-shadow: 
    0 25px 80px rgba(0, 0, 0, 0.9),
    0 0 0 1px rgba(102, 126, 234, 0.2),
    inset 0 1px 0 rgba(255, 255, 255, 0.1);
}

.logo-icon {
  text-align: center;
  margin-bottom: 20px;
}

.logo-circle {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background: linear-gradient(135deg, #667eea, #764ba2);
  margin: 0 auto;
  position: relative;
  animation: logo-pulse 3s ease-in-out infinite;
}

.logo-circle::before {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
  animation: logo-inner 3s ease-in-out infinite reverse;
}

@keyframes logo-pulse {
  0%, 100% {
    transform: scale(1);
    box-shadow: 0 0 20px rgba(102, 126, 234, 0.3);
  }
  50% {
    transform: scale(1.05);
    box-shadow: 0 0 30px rgba(102, 126, 234, 0.5);
  }
}

@keyframes logo-inner {
  0%, 100% {
    transform: translate(-50%, -50%) scale(1);
  }
  50% {
    transform: translate(-50%, -50%) scale(0.8);
  }
}

h2 {
  text-align: center;
  margin-bottom: 35px;
  color: #ffffff;
  font-size: 26px;
  font-weight: 600;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.5);
  letter-spacing: 1px;
}

.login-button {
  width: 100%;
  height: 48px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
  color: #ffffff;
  font-weight: 600;
  font-size: 16px;
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
  border-radius: 8px;
}

.login-button:hover {
  background: linear-gradient(135deg, #5a6fd8 0%, #6a4190 100%);
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(102, 126, 234, 0.4);
}

.button-text {
  position: relative;
  z-index: 2;
}

.button-glow {
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
  transition: left 0.5s ease;
}

.login-button:hover .button-glow {
  left: 100%;
}

.register-link {
  text-align: center;
  width: 100%;
  color: rgba(255, 255, 255, 0.7);
  cursor: pointer;
  transition: all 0.3s ease;
  font-size: 14px;
}

.register-link:hover {
  color: rgba(255, 255, 255, 0.9);
}

.highlight {
  color: #667eea;
  font-weight: 600;
  transition: color 0.3s ease;
}

.register-link:hover .highlight {
  color: #5a6fd8;
}

.brand-mark {
  position: absolute;
  top: 50px;
  left: 0;
  right: 0;
  text-align: center;
  color: #667eea;
  font-size: 38px;
  font-weight: 700;
  letter-spacing: 6px;
  text-shadow: 
    0 0 10px rgba(102, 126, 234, 0.8),
    0 0 20px rgba(102, 126, 234, 0.6),
    0 0 30px rgba(102, 126, 234, 0.4);
  pointer-events: none;
  margin: 0 auto;
  width: fit-content;
  animation: text-glow 4s ease-in-out infinite alternate;
  z-index: 5;
}

@keyframes text-glow {
  from {
    text-shadow: 
      0 0 10px rgba(102, 126, 234, 0.8),
      0 0 20px rgba(102, 126, 234, 0.6),
      0 0 30px rgba(102, 126, 234, 0.4);
  }
  to {
    text-shadow: 
      0 0 5px rgba(102, 126, 234, 0.6),
      0 0 15px rgba(102, 126, 234, 0.4),
      0 0 25px rgba(102, 126, 234, 0.2);
  }
}

/* 暗黑风格输入框（加强覆盖，含 autofill） */
:deep(.custom-input .el-input__wrapper) {
  --el-input-bg-color: #0f0f12;
  background: #0f0f12 !important;
  background-color: #0f0f12 !important;
  border: none !important;
  box-shadow: none !important;
  transition: all 0.3s ease;
  border-radius: 8px;
  height: 48px;
}

:deep(.custom-input .el-input__wrapper:hover) {
  box-shadow: 0 0 12px rgba(102, 126, 234, 0.15);
  transform: translateY(-1px);
}

:deep(.custom-input .el-input__wrapper.is-focus) {
  box-shadow: 0 0 0 1px rgba(102, 126, 234, 0.25), 0 0 16px rgba(102, 126, 234, 0.15);
  transform: translateY(-1px);
}

:deep(.custom-input .el-input__inner) {
  color: #ffffff;
  background: #0f0f12 !important;
  background-color: #0f0f12 !important;
  font-size: 14px;
  border: none !important;
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
  -webkit-text-fill-color: #ffffff;
  caret-color: #ffffff;
}

:deep(.custom-input input:-webkit-autofill),
:deep(.custom-input input:-webkit-autofill:hover),
:deep(.custom-input input:-webkit-autofill:focus) {
  -webkit-text-fill-color: #ffffff !important;
  box-shadow: 0 0 0px 1000px #0f0f12 inset !important;
  background-color: #0f0f12 !important;
  transition: background-color 9999s ease-out 0s;
}

:deep(.custom-input .el-input__inner::placeholder) {
  color: rgba(255, 255, 255, 0.55);
}

:deep(.custom-input .el-input__prefix) {
  color: rgba(102, 126, 234, 0.85);
  font-size: 16px;
}

/* 响应式设计 */
@media (max-width: 480px) {
  .login-container {
    width: 90%;
    padding: 40px 30px;
  }
  
  .brand-mark {
    font-size: 28px;
    letter-spacing: 4px;
    top: 30px;
  }
  
  h2 {
    font-size: 22px;
  }
}
</style> 